package com.keeping.aiimagery.controller;


import cn.hutool.json.JSONUtil;
import com.keeping.aiimagery.entity.LoginDto;
import com.keeping.aiimagery.entity.UpdataVo;
import com.keeping.aiimagery.entity.User;
import com.keeping.aiimagery.service.InviteService;
import com.keeping.aiimagery.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author keeping
 * @since 2023-04-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private InviteService inviteService;

    //获取账号积分
    @GetMapping("/getIntergral")
    @ResponseBody
    public Integer getIntergral(@RequestParam("userAccount") String userAccount){
        return userService.getIntegralById(userAccount);
    }

    //获取id
    @GetMapping("/getIdByUserAccount")
    @ResponseBody
    public Integer getIdByUserAccount(@RequestParam("userAccount") String userAccount){
        return userService.findByAccount(userAccount);
    }

    @GetMapping("/{phone}")
    public boolean checkphone(@PathVariable("phone") String phone)
    {
        return userService.findphone(phone);
    }

    @GetMapping("/getmsg")
    public User getmsg(@RequestParam("phone") String phone)
    {
        return userService.getmsg(phone);
    }
    //账号密码登录
    @PostMapping("/login")
    public String userLogin(@RequestBody Map<String,String > data) {
        HashMap<String, String> result;

        System.out.println(data);
        String userAccount = data.get("userAccount");
        String password = data.get("password");
        result = userService.userLogin(userAccount, password);
        System.out.println(result);
        return JSONUtil.toJsonStr(result);
    }
    /*
     *   注册，建议修改成RequeBody试一试。直接包装成类较好
     * */
    @GetMapping("/register")
    public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("phone") String phone,@RequestParam(value = "invitationCode",required = false) String invitationCode)
    {

        String uniqueCode = UUID.randomUUID().toString();
        String invitationCode1=uniqueCode.substring(0,6);
        userService.insert(username,password,phone,invitationCode1);

        Integer userId = null;
        if(invitationCode!="")
        {
            userId=userService.findByYaoqingma(invitationCode);
            if(userId!=null)
            {
                UpdataVo updataVo=new UpdataVo();
                updataVo.setId(userId);
                updataVo.setIntegral(10);
                userService.updataByInvite(updataVo);

                String account = phone;
                int id = userService.findByAccount(account);

                inviteService.insertInvite(userId,id);
            }
        }

        System.out.println(username+' '+password+' '+phone+' '+invitationCode);
        return "OK";
    }
    //个人信息获取
    @GetMapping("/getUser")
    public User findUser(@RequestParam("userAccount") String userAccount){
        return userService.findUser(userAccount);
    }

    @GetMapping("/updateconsume")
    public void updateconsume(@RequestParam("integral") Integer integral,@RequestParam("remain") Integer remain,@RequestParam("userId") String userId)
    {
        userService.updateconsume(integral,remain,userId);
    }
    @GetMapping("/updateconsumecount")
    public void updateconsumecount(@RequestParam("integral") Integer integral,@RequestParam("remaincount") Integer remaincount,@RequestParam("userId") String userId)
    {
        userService.updateconsumecount(integral,remaincount,userId);
    }

    @GetMapping("/updateconsumestorage")
    public void updateconsumestorage(@RequestParam("integral") Integer integral,@RequestParam("remainstorage") Integer remainstorage,@RequestParam("userId") String userId)
    {
        userService.updateconsumestorage(integral,remainstorage,userId);
    }
}

